package cn.xiaosiyuan.util.network;

import cn.xiaosiyuan.util.X;

import java.io.IOException;
import java.net.*;

/**
 *
 * 网络校验工具类
 * {@link NetWorkUtil#telNet}   没开通，会报错
 * {@link NetWorkUtil#url}      没开通，会报错
 * {@link NetWorkUtil#ping}     没开通，不会报错
 *
 */
public class NetWorkUtil {

    /**
     * 测试网络是否可达
     * <p>
     * 一般在开启防火墙的时候，用来校验网络策略是否开通
     * <p>
     * 类似于执行 telnet 命令
     *
     * @param ip      ip地址
     * @param host    端口号
     * @param timeout 连接超时时间，单位秒
     * @return 是否可连接
     */
    public static boolean telNet(String ip, Integer host, Integer timeout) {
        boolean res = false;
        Socket connect = new Socket();
        try {
            connect.connect(new InetSocketAddress(ip, host), timeout * 1000);
            res = connect.isConnected();
            if (res) {
                X.loggerUtil.info("ip和端口号连接成功！" + ip + ":" + host);
            } else {
                X.loggerUtil.error("ip和端口号连接失败！请检查连接：" + ip + ":" + host);
            }
        } catch (Exception e) {
            e.printStackTrace();
            X.loggerUtil.error("ip和端口号连接失败！请检查连接：" + ip + ":" + host);
            X.loggerUtil.error(e.getMessage());
        }
        return res;
    }

    /**
     *
     * 测试url是否可达
     * http://192.168.1.134:8089/jtj_war_exploded/
     *
     *
     * http://
     * 不指定的协议会报错
     * https://blog.csdn.net/qq_35246620/article/details/70162106
     *
     *
     * @param url      url地址
     * @param timeout 连接超时时间，单位秒
     * @return 是否可连接
     */
    public static boolean url(String url, Integer timeout) {
        boolean res = false;
        int status = Status.FAILURE.value;

        try {
            URL curUrl = new URL(url);
            HttpURLConnection oc = (HttpURLConnection) curUrl.openConnection();
            oc.setUseCaches(false);
            // 设置超时时间 timeout 秒
            oc.setConnectTimeout(timeout * 1000);
            // 请求状态
            status = oc.getResponseCode();
            if (Status.SUCCESS.value == status) {
                X.loggerUtil.info("url连接成功！" + url);
                res = true;
            } else {
                X.loggerUtil.error("url连接失败！请检查连接：" + url);
                X.loggerUtil.error(status + "");
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
            X.loggerUtil.error("url连接失败！请检查连接：" + url);
            X.loggerUtil.error(e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
            X.loggerUtil.error("url连接失败！请检查连接：" + url);
            X.loggerUtil.error(e.getMessage());
        }
        return res;
    }

    enum Status {
        FAILURE(404, "请求失败"),
        SUCCESS(200, "请求成功");

        public int value;
        public String describe;

        Status(int value, String describe) {
            this.value = value;
            this.describe = describe;
        }
    }


    /**
     *
     * 测试ip是否可连接
     * <p>
     * 一般在开启防火墙的时候，用来校验网络策略是否开通
     * <p>
     * 类似于执行 ping 命令
     *
     * @param ip      ip地址
     * @param timeout 连接超时时间，单位秒
     * @return
     */
    public static boolean ping(String ip, Integer timeout) {
        boolean res = false;

        InetAddress address;

        try {
            address = InetAddress.getByName(ip);
            boolean isReachable = address.isReachable(timeout * 1000);
            res = isReachable;
            if (res) {
                X.loggerUtil.info("ip连接成功！");
                X.loggerUtil.info("name: " + address.getHostName());
                X.loggerUtil.info("addr: " + address.getHostAddress());
                X.loggerUtil.info("isIpReachable: " + isReachable);
            } else {
                X.loggerUtil.error("ip连接失败！");
                X.loggerUtil.error("name: " + address.getHostName());
                X.loggerUtil.error("addr: " + address.getHostAddress());
                X.loggerUtil.error("isIpReachable: " + isReachable);
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
            X.loggerUtil.error("ip连接失败！请检查连接：" + ip);
            X.loggerUtil.error(e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
            X.loggerUtil.error("ip连接失败！请检查连接：" + ip);
            X.loggerUtil.error(e.getMessage());
        }
        return res;
    }

}
